<template>
  <el-card>
    <el-table style="margin: 20px 0" border stripe :data="skuArr">
      <el-table-column
        label="序号"
        type="index"
        align="center"
        width="100px"
      ></el-table-column>
      <el-table-column
        label="名称"
        align="center"
        show-overflow-tooltip
        width="200px"
        prop="skuName"
      ></el-table-column>
      <el-table-column
        label="描述"
        align="center"
        show-overflow-tooltip
        min-width="200px"
        prop="skuDesc"
      ></el-table-column>
      <el-table-column label="图片" align="center" width="200px">
        <template #="{ row, index }">
          <img :src="row.skuDefaultImg" alt="" style="width: 200px"/>
        </template>
      </el-table-column>
      <el-table-column
        label="重量"
        align="center"
        width="200px"
        prop="weight"
      ></el-table-column>
      <el-table-column
        label="价格"
        align="center"
        width="200px"
        prop="price"
      ></el-table-column>
      <el-table-column label="操作" align="center" width="250px" fixed="right">
        <template #="{ row, index }">
          <el-button
            :type="row.isSale === 1 ? 'info' : 'success'"
            size="small"
            :icon="row.isSale === 1 ? 'Bottom' : 'Top'"
            @click="onUpdateSale(row)"
          ></el-button>
          <el-button
            type="primary"
            size="small"
            icon="Edit"
            @click="onEdit(row)"
          ></el-button>
          <el-button
            type="info"
            size="small"
            icon="InfoFilled"
            @click="onFindSku(row)"
          ></el-button>
          <el-popconfirm
            :title="`你确定要删除${row.skuName}?`"
            width="200px"
            @confirm="onDelete(row)"
          >
            <template #reference>
              <el-button type="danger" size="small" icon="Delete"></el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!--分页器-->
    <el-pagination
      v-model:current-page="pageNo"
      v-model:page-size="pageSize"
      :background="true"
      :page-sizes="[10, 20, 30, 40]"
      layout=" prev, pager, next, jumper,->,total, sizes"
      :total="total"
      @current-change="getHasSku"
      @size-change="handler"
    />
    <!--抽屉-->
    <el-drawer v-model="drawer">
      <template #header>
        <h4>查看商品详情</h4>
      </template>
      <template #default>
        <el-row style="padding: 5px 0">
          <el-col :span="6">名称</el-col>
          <el-col :span="18">{{ skuInfo.skuName }}</el-col>
        </el-row>
        <el-row style="padding: 5px 0">
          <el-col :span="6">描述</el-col>
          <el-col :span="18">{{ skuInfo.skuDesc }}</el-col>
        </el-row>
        <el-row style="padding: 5px 0">
          <el-col :span="6">价格</el-col>
          <el-col :span="18">{{ skuInfo.price }}</el-col>
        </el-row>
        <el-row style="padding: 5px 0; align-items: center">
          <el-col :span="6">平台属性</el-col>
          <el-col :span="18">
            <el-tag
              v-for="item in skuInfo.skuAttrValueList"
              style="margin: 5px"
              :key="item.id"
            >
              {{ item.valueName }}
            </el-tag>
          </el-col>
        </el-row>
        <el-row style="padding: 5px 0; align-items: center">
          <el-col :span="6">销售属性</el-col>
          <el-col :span="18">
            <el-tag
              v-for="item in skuInfo.skuSaleAttrValueList"
              style="margin: 5px"
              :key="item.id"
            >
              {{ item.saleAttrName }}
            </el-tag>
          </el-col>
        </el-row>
        <el-row style="padding: 5px 0; align-items: center">
          <el-col :span="6">商品图片</el-col>
          <el-col :span="18">
            <el-carousel :interval="4000" type="card" height="200px">
              <el-carousel-item
                v-for="item in skuInfo.skuImageList"
                :key="item"
              >
                <img :src="item.imgUrl" alt="" style="width: 100%"/>
              </el-carousel-item>
            </el-carousel>
          </el-col>
        </el-row>
      </template>
    </el-drawer>
  </el-card>
</template>
<script setup lang="ts">
import {onMounted, ref} from 'vue'
import {
  reqCancelSale,
  reqRemoveSku,
  reqSaleSku,
  reqSkuInfo,
  reqSkuList,
} from '@/api/product/sku'
import type {
  SkuData,
  SkuInfoData,
  SkuResponseData,
} from '@/api/product/sku/type'
import {ElMessage} from 'element-plus'

let pageNo = ref<number>(1)
let pageSize = ref<number>(10)
let total = ref<number>(0)
let skuArr = ref<SkuData[]>([])
// 显示、隐藏抽屉
let drawer = ref<boolean>(false)
let skuInfo = ref<SkuData>({})

onMounted(() => {
  getHasSku()
})
const getHasSku = async (pager = 1) => {
  pageNo.value = pager
  let result: SkuResponseData = await reqSkuList(pageNo.value, pageSize.value)
  if (result.code === 200) {
    total.value = result.data.total
    skuArr.value = result.data.records
  }
}
const handler = (pageSizes: number) => {
  getHasSku()
}
// 商品上架、下架
const onUpdateSale = async (row: SkuData) => {
  if (row.isSale === 1) {
    // 下架
    await reqCancelSale(row.id)
    ElMessage({
      type: 'success',
      message: '下架成功',
    })
    await getHasSku()
  } else {
    await reqSaleSku(row.id)
    ElMessage({
      type: 'success',
      message: '上架成功',
    })
    await getHasSku()
  }
}
// 点击修改
const onEdit = (row: SkuData) => {
  ElMessage({
    type: 'warning',
    message: '正在开发中',
  })
}
// 点击查看
const onFindSku = async (row: SkuData) => {
  drawer.value = true
  let result: SkuInfoData = await reqSkuInfo(row.id)
  if (result.code === 200) {
    skuInfo.value = result.data
  }
}
// 点击删除
const onDelete = async (row: SkuData) => {
  let result: any = await reqRemoveSku(row.id)
  if (result.code === 200) {
    ElMessage({
      type: 'success',
      message: '删除成功',
    })
    await getHasSku(skuArr.value.length > 1 ? pageNo.value : pageNo.value - 1)
  } else {
    ElMessage({
      type: 'error',
      message: result.data,
    })
  }
}
</script>
<style scoped lang="scss">
.el-carousel__item h3 {
  color: #475669;
  opacity: 0.75;
  line-height: 200px;
  margin: 0;
  text-align: center;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
</style>
